<?php
defined('BASEPATH') OR exit('No direct script access allowed');
 
class IndexLarge extends AdminController {

	function __construct() {
		parent::__construct();
		// $this -> sidebar = "Overview-Home";
		$this -> load -> model("Classes_number_model");
		$this -> load -> model("Image_text_model");
	}

	//专项听课次数配置
	function classes_number() {
		$this->load->view('admin/classesnumberlist');
	}

	function classes_number_getDatas($params = array(), $isExport = false) {
		$connar = array(); 
		if (!$isExport) {
			$params = $_POST;
			$connar = $this -> Classes_number_model -> pickPages($connar, $params);
		}
		$connar['orderby'] = "created desc";
		$connar['where']="1=1";
		if (isset($params['param'])) {
			$connar['where'] .= $this->sqlLikeEscape(" and title like '%??%' ",array($params['param']));
		}
		$connar['where'] .=" and status=1";

		if (!$isExport) {
			$data = $this -> Classes_number_model -> get_page_list($connar);
		} else {
			$data["ret"] = $this -> Classes_number_model -> get_list_full($connar);
		}
		foreach ($data["ret"] as $k => $v) {
			$data['ret'][$k]['created_char']=date('Y-m-d H:i:s',$v['created']);
			$data['ret'][$k]['updated_char']=date('Y-m-d H:i:s',$v['updated']);
		}

		if (!$isExport) {
			echo json_encode($data);
		} else {
			return $data["ret"];
		}
	}

	//新增，修改
	function classes_number_edit($id){
		if($id == 0){
			$data['title'] = "新增专项听课次数";
			$data['detail'] = $this -> Classes_number_model -> get_single(array("id" => $id));
		}else{
			$data['title'] = "编辑专项听课次数";
			$data['detail'] = $this -> Classes_number_model -> get_single(array("id" => $id));
		}
		$this -> load -> view('admin/classesnumberEdit',$data);
	}
	//新增修改操作
	function classes_number_save(){
		// $_POST=$this->NOTXSS_POST;

		if (!isset($_POST['title'])) {
			exit(retJson("请填写标题.", false));
		}
		if (!isset($_POST['number'])) {
			exit(retJson("请填写次数.", false));
		}
		
		$announcement = copyArray($_POST,array("id","title",'number'));
		if(!isset($_POST['id']) || $_POST['id'] == ""){
			$announcement['status']=CommonStatus::UnDeleted[XPHEnum::Value];
			$announcement['creator']=getSess()['id'];
			$announcement['created']=time();
			$this -> Classes_number_model -> add($announcement);
			$isAdd = 1;
			exit(retJson("添加成功.", true,$isAdd));
		}else{		
			$isAdd = 0;
			$announcement['updated']=time();
			$this -> Classes_number_model -> update($announcement);

			exit(retJson("修改成功.", true,$isAdd));
		}
	}
	//删除次数
	function classes_number_delete($id){
		$this->Classes_number_model->update(array('id'=>$id,'status'=>CommonStatus::Deleted[XPHEnum::Value]));
		exit(retJson("删除成功.", true));
	}
	

	//图文配置

	public function imagetext() { 
		$this -> load -> view('admin/imagetextList');
	}
	
	
	//图文配置
	public function imagelist() {
		
        $params = $_GET;
        $params['role'] = getSess()['role_id'];
		$this -> load -> view('admin/imageList',$params);
	}


	function imagetext_getDatas($params = array(), $isExport = false) {
		$connar = array(); 
		if (!$isExport) {
			$params = $_POST;
			$connar = $this -> Image_text_model -> pickPages($connar, $params);
		}
		$connar['items']= " id,title,type,created,file_url";
		$connar['orderby'] = "created desc";
		$connar['where']="1=1";
		if (isset($params['param'])) {
			$connar['where'] .= $this->sqlLikeEscape(" and title like '%??%' ",array($params['param']));
			// $connar['where'] .= $this->sqlLikeEscape(" or subtitle like '%??%') ",array($params['param']));
		}
		if(isset($params['type']) && $params['type'] != "-1")
		{
			$connar['where'] .= $this->sqlLikeEscape(" and type=?? ",array($params['type']));			
		}
		$connar['where'] .=" and status=1";
		
		if (!$isExport) {
			$data = $this -> Image_text_model -> get_page_list($connar);
		} else {
			$data["ret"] = $this -> Image_text_model -> get_list_full($connar);
		}
		foreach ($data["ret"] as $k => $v) {
			// $data['ret'][$k]['img_input']='<img src="'.$v['image'].'" style="width:100px; height:50px;">';
			$data['ret'][$k]['typeChar']=  (new ImageType)->show($v['type']);
			$data['ret'][$k]['createdChar']=date('Y-m-d H:i:s',$v['created']);
		}

		$data["ret"]=array_values($data["ret"]);
		if (!$isExport) {
			echo json_encode($data);
		} else {
			return $data["ret"];
		}
	}
	
	
	
	//新增，修改
	function imagetext_edit($id){
		
		if($id == 0){
			$data['title'] = "新增文章";
			$data['detail'] = $this -> Image_text_model -> get_single(array("id" => $id));
		}else{
			$data['title'] = "查看/编辑文章";
			$data['detail'] = $this -> Image_text_model -> get_single(array("id" => $id));
		 	
		}
		$this -> load -> view('admin/imagetextEdit',$data);
	}
	function imagetext_save(){
		$_POST=$this->NOTXSS_POST;
		if(($_POST['type'] == 3 || $_POST['type'] == 4) && isset($_POST['file_url']) && isset($_POST['file_name']) && $_POST['file_url'] == ""){
			exit(retJson("请上传pdf文件.", false));
		}
		
		$goods = copyArray($_POST,array("title","type","id","content"));
	
		if(isset($_POST['image_url']) && $_POST['image_url'] != ""){ //存在图片时
			$goods['image_url'] = $_POST['image_url'];
		}

		if(isset($_POST['file_url']) && isset($_POST['file_name'])){ //存在文件时
			$goods['file_url'] = $_POST['file_url'];	
			$goods['filename'] = $_POST['file_name'];	
		}
		
		if(!isset($_POST['id']) || $_POST['id'] == ""){
			// $count = $this -> Image_text_model -> get_count(array("title"=>$_POST['title']));
			// if($count)
			// {
			// 	exit(retJson("该文章已存在.", false));
			// }
			$goods['created']=time();
			$this -> Image_text_model -> add($goods);
			$isAdd = 1;


			exit(retJson("添加成功.", true,$isAdd));
		}else{
			
			$goods_data=$this->Image_text_model->get_single(array('id'=>$_POST['id']));
			if($goods_data['image_url']&&$goods_data['image_url']!=$goods['image_url'])
			{
				unlink($goods_data['image_url']);
			}

			if($goods_data['file_url']&&$goods_data['file_url']!=$goods['file_url'])
			{
				unlink($goods_data['file_url']);
			}
			
			$goods['updated']=time();
			$isAdd = 0;
			$this -> Image_text_model -> update($goods);
			exit(retJson("修改成功.", true,$isAdd));
		}
	}


	//改变启用状态
	// function changeStatus($id, $status) {
	// 	$admin = $this -> Article_model -> get_single(array("id" => $id));
	// 	if (!$admin) {
	// 		exit(retJson("当前文章不存在，请刷新后重试.", false));
	// 	}
	// 	// var_dump($admin);
	// 	// exit();
	// 	if ($admin['status']!=$status) {
	// 		exit(retJson("当前文章状态不正确，请刷新后重试.", false));
	// 	}

	// 	$admin['status'] = 1 - $status;
	// 	$this -> Article_model -> update($admin);
	// 	$char=$status?'隐藏':'发布';
	// 	exit(retJson("已" .$char.'文章【'.$admin["title"].'】', true));
	// }

	function imagetext_delete($id){
		$this->Image_text_model->update(array('id'=>$id,'status'=>CommonStatus::Deleted[XPHEnum::Value]));
		exit(retJson("删除成功.", true));
	}
	


	

}
